import axios from "axios";
import {Dialog, Toast} from "vant";


const service = axios.create({
    baseURL: 'https://xxlmapi.xk857.com',
    timeout: 5000
})


service.interceptors.request.use(config => {
        if (config.loading) {
            Toast.loading({
                duration: 0,
                message: '加载中...',
                forbidClick: true,
            });
        }

        const token = localStorage.getItem("token");
        if (token) {
            config.headers['token'] = token
        }
        return config
    }, error => {
        Dialog.alert({
            title: "提示",
            message: "网络请求失败，请刷新重试"
        });
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(response => {
        Toast.clear()
        const res = response.data
        if (res.code !== 20000) {
            Dialog.alert({
                title: "提示",
                message: res.msg || '发生错误'
            });
            return Promise.reject(new Error(res.message || 'Error'))
        } else {
            return res.data
        }
    }, error => {
        Toast.clear()
        Dialog.alert({
            title: "提示",
            message: "网络请求失败"
        });
        return Promise.reject(error)
    }
)
export default service
